package org.jeecg.modules.demo.yxkcqcorder.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.utils.generateOddUtil;
import org.jeecg.modules.demo.warehouseorder.entity.WarehouseOrder;
import org.jeecg.modules.demo.warehouseorder.mapper.WarehouseOrderMapper;
import org.jeecg.modules.demo.yxkcqcorder.entity.YxKcqcOrder;
import org.jeecg.modules.demo.yxkcqcorder.mapper.YxKcqcOrderMapper;
import org.jeecg.modules.demo.yxkcqcorder.service.IYxKcqcOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: yx_kcqc_order
 * @Author: jeecg-boot
 * @Date:   2022-11-10
 * @Version: V1.0
 */
@Service
public class YxKcqcOrderServiceImpl extends ServiceImpl<YxKcqcOrderMapper, YxKcqcOrder> implements IYxKcqcOrderService {
    @Resource
    private YxKcqcOrderMapper yxKcqcOrderMapper;
    @Resource
    private WarehouseOrderMapper warehouseOrderMapper;

    @Override
    public String getMaxCode(){
        return yxKcqcOrderMapper.getMaxCode();
    }

    @Override
    @Transactional
    public Boolean checkQcOrder(YxKcqcOrder yxKcqcOrder,String precode){
        try{
            String str = warehouseOrderMapper.getMaxCode();
            Map<String,String> map = new HashMap<>();
            if(str==null){
                map.put("maxCode",null);
            }else{
                map.put("maxCode",str);
            }
            String name =precode+ "RK_";
            String resultCode = generateOddUtil.createCode(map,name);
            WarehouseOrder warehouseOrder = createWareHouseOrder(yxKcqcOrder,resultCode);
            yxKcqcOrder.setWoNo(warehouseOrder.getWoNo());
            warehouseOrderMapper.insert(warehouseOrder);
            yxKcqcOrderMapper.updateById(yxKcqcOrder);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }







    @Override
    @Transactional
    public Boolean unCheckQcOrder(YxKcqcOrder yxKcqcOrder){
        try{

            WarehouseOrder warehouseOrder = warehouseOrderMapper.getByWoNo(yxKcqcOrder.getWoNo());
            warehouseOrderMapper.deleteById(warehouseOrder.getId());
            yxKcqcOrder.setShenhe(0);
            yxKcqcOrder.setShTime(null);
            yxKcqcOrder.setWoNo(null);
            yxKcqcOrderMapper.updateById(yxKcqcOrder);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }
    /**
     * 生成其他入库单
     * @param
     * @return
     */
    public WarehouseOrder createWareHouseOrder(YxKcqcOrder yxKcqcOrder, String code){
        WarehouseOrder warehouseOrder = new WarehouseOrder();
        warehouseOrder.setTradecode(yxKcqcOrder.getTradecode());
        warehouseOrder.setChumdwmc(yxKcqcOrder.getChumdwmc());
        warehouseOrder.setWoNo(code);
        warehouseOrder.setPuWoNo(code);
        warehouseOrder.setProWhNum(yxKcqcOrder.getWhNum());
        warehouseOrder.setWhNum(yxKcqcOrder.getWhNum());
        warehouseOrder.setQoQuantity(yxKcqcOrder.getQoQuantity());
        warehouseOrder.setWoDate(yxKcqcOrder.getWoDate());
        warehouseOrder.setCreateTime(new Date());
        warehouseOrder.setPuWhNum(yxKcqcOrder.getWhNum());
        warehouseOrder.setShenhe(1);
        warehouseOrder.setSupsttelemnt(yxKcqcOrder.getRkfs()==4?yxKcqcOrder.getQoQuantity():new BigDecimal(0.000));
        warehouseOrder.setPsTax(yxKcqcOrder.getPsTax());
        warehouseOrder.setFtse(yxKcqcOrder.getPsTax());
        warehouseOrder.setPsTaxrat(yxKcqcOrder.getPsTaxrat());
        warehouseOrder.setSaleman(yxKcqcOrder.getSaleman());
        warehouseOrder.setShanchu(0);
        warehouseOrder.setPoTotail(yxKcqcOrder.getQoQuantity());
        warehouseOrder.setFtwsdj(yxKcqcOrder.getWsdj());
        warehouseOrder.setWsdj(yxKcqcOrder.getWsdj());
        warehouseOrder.setFtwsje(yxKcqcOrder.getWsje());
        warehouseOrder.setWsje(yxKcqcOrder.getWsje());
        warehouseOrder.setStandingstock(yxKcqcOrder.getQoQuantity());
        warehouseOrder.setMeasureunit(yxKcqcOrder.getMeasureunit());
        warehouseOrder.setDepartment(yxKcqcOrder.getDepartment());
        warehouseOrder.setPoStaff(yxKcqcOrder.getPoStaff());
        warehouseOrder.setRklb(yxKcqcOrder.getRkfs());//期初
        warehouseOrder.setCinvname(yxKcqcOrder.getCinvname());
        warehouseOrder.setRealzb(yxKcqcOrder.getRealzb());
        warehouseOrder.setProject(yxKcqcOrder.getProject());
        warehouseOrder.setHsje(yxKcqcOrder.getHsje());
        warehouseOrder.setFthsje(yxKcqcOrder.getHsje());
        warehouseOrder.setHsdj(yxKcqcOrder.getHsdj());
        warehouseOrder.setFthsdj(yxKcqcOrder.getHsdj());
        warehouseOrder.setFttotalcost(yxKcqcOrder.getHsje());
        warehouseOrder.setMeasureunit("吨");
        warehouseOrder.setIsSettlement(yxKcqcOrder.getRkfs()==4?0:3);
        warehouseOrder.setZhilzb(yxKcqcOrder.getRealzb());
        warehouseOrder.setFpje(new BigDecimal(0.000));
        warehouseOrder.setFpsl(new BigDecimal(0.000));
        warehouseOrder.setFpdj(new BigDecimal(0.000));
        warehouseOrder.setZgsl(warehouseOrder.getQoQuantity());
        warehouseOrder.setZxcb(warehouseOrder.getWsje());
        warehouseOrder.setShTime(new Date());
        warehouseOrder.setCreateBy(yxKcqcOrder.getCreateBy());
        warehouseOrder.setSysOrgCode(yxKcqcOrder.getSysOrgCode());
        return warehouseOrder;
    }


}
